package
{
	import com.snda.sng.chat.ChatClient;
	import com.snda.sng.chat.events.ChatClientErrorEvent;
	import com.snda.sng.chat.events.ChatClientMessageEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.net.Socket;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.ui.Keyboard;
	import flash.utils.getTimer;
	
	
	/**
	 * ChatTestMain
	 * @author 任冬 rending01@snda.com
	 * $Id: ChatTestMain.as 28 2011-07-22 02:59:01Z rendong $
	 * @version 1.0
	 */
	[SWF(frameRate=24, height=300, width=400)] 
	public class ChatTestMain extends Sprite
	{
		/**
		 * 聊天客户端对象 
		 */		
		private var client:ChatClient;
		
		private var txt_message:TextField;
		private var txt_input:TextField;
		
		public function ChatTestMain()
		{
			txt_message = new TextField();
			txt_message.text = "请输入昵称：";
			txt_message.y = 150;
			txt_message.autoSize = TextFieldAutoSize.LEFT;
			addChild(txt_message);
			
			txt_input = new TextField();
			
			txt_input.type = TextFieldType.INPUT;
			txt_input.width =100;
			txt_input.height = txt_message.height;
			txt_input.border = true;
			txt_input.x = txt_message.width + 10;
			txt_input.y = txt_message.y;
			addChild(txt_input);
			txt_input.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
			
			stage.focus = txt_input;
		}
		
		private function initChat(name:String):void
		{
			txt_input.removeEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
			removeChild(txt_message);
			removeChild(txt_input);
			
			txt_message = new TextField();
			txt_message.multiline = true;
			txt_message.background = true;
			txt_message.border = true;
			txt_message.width = stage.stageWidth;
			txt_message.height = stage.stageHeight - 50;
			addChild(txt_message);
			
			var txt:TextField = new TextField();
			txt.text = "请输入聊天";
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.y = stage.stageHeight - 50 + 20;
			addChild(txt);
			
			txt_input = new TextField();
			txt_input.type = TextFieldType.INPUT;
			txt_input.width = stage.stageWidth - txt.width - 20;
			txt_input.height = txt.height;
			txt_input.border = true;
			txt_input.x = txt.width + 10;
			txt_input.y = txt.y;
			addChild(txt_input);
			txt_input.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
			stage.focus = txt_input;
			
			// 以下为测试参数
			var uid:String = "uid_" + name ;
			var name:String = name;
			var password:String = "password";
			var host:String = "10.240.66.51";
			var port:int = 3128;			
			
			var fids:Vector.<String> = new Vector.<String>();
			for(var i:int = 0; i< 50; i++)
			{
				fids.push("userid_" + i);
			}
			
			
			// 创建chat客户端
			client = new ChatClient(uid, name, password, host, port);
			
			// 初始化事件
			client.addEventListener(ChatClientMessageEvent.EventType, messageHandler);			
			client.addEventListener(ChatClientErrorEvent.EventType, errorHandler);
			
			for(i = 0; i< fids.length; i++)
			{
				client.addFriendID(fids[i]);
			}
			
			client.connect();
		}
		
		
		/**
		 * 显示消息 
		 * @param message
		 * 
		 */		
		private function showMessage(message:String):void
		{
			txt_message.htmlText += message+"\n";
			txt_message.scrollV = txt_message.maxScrollV;
		}
		
		// 回车
		private function keyHandler(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.ENTER)
			{
				if (txt_input.text.length > 0){
					if (client == null)
					{
						initChat(txt_input.text);
					}else{
						client.sendChannelMsg(ChatClient.CHANNEL_WORLD, txt_input.text);
						txt_input.text = "";
					}
				}
			}
		}
		
		// 错误
		private function errorHandler(e:ChatClientErrorEvent):void
		{
			switch(e.errorType)
			{
				case ChatClientErrorEvent.ERROR_AUTH_FAIL:
					trace("error:认证失败!");
					showMessage("error:认证失败!");
					break;
				case ChatClientErrorEvent.ERROR_IOERROR:
					trace("error:网络错误");
					showMessage("error:网络错误,重试连接中...");
					break;
				case ChatClientErrorEvent.ERROR_SECURITY:
					trace("error:安全错误");
					showMessage("error:安全错误");
					break;
			}
		}
		
		// 消息处理
		private function messageHandler(e:ChatClientMessageEvent):void
		{
			switch(e.MsgType)
			{
				case ChatClientMessageEvent.TYPE_Auth:
					showMessage("连接服务器成功");
					break;
				
				case ChatClientMessageEvent.TYPE_Friend_Online_List:
					trace("在线好友列表：", e.onlineFriendsList);
					break;
				
				case ChatClientMessageEvent.TYPE_Friend_Status_Online:
					trace("好友状态切换:", e.FriendID, "上线");
					break;
				case ChatClientMessageEvent.TYPE_Friend_Status_Offline:
					trace("好友状态切换:", e.FriendID, "下线");
					break;
				
				case ChatClientMessageEvent.TYPE_Channel_Success:
					trace("加入频道：", e.ChannelName, "成功");
					break;
				case ChatClientMessageEvent.TYPE_Channel_Fail:
					trace("加入频道：", e.ChannelName, "失败");
					break;
				
				case ChatClientMessageEvent.TYPE_Msg_Channel:
					trace("频道信息：", e.Name, e.srcUserName, e.Message);
					if (e.Name == ChatClient.CHANNEL_WORLD)
						e.Name = "世界";
					showMessage("频道:<font color=\"#00FFFF\">"+ e.Name+ "</font> <font color=\"#0000FF\">" + e.srcUserName+ "</font> 说："+ e.Message);
					break;
				case ChatClientMessageEvent.TYPE_Msg_Private:
					trace("私聊信息：", e.Name, e.srcUserName, e.Message);
					break;
				case ChatClientMessageEvent.TYPE_Msg_Admin:
					trace("管理信息：", e.Name, e.Message);
					showMessage("后台发送:类型<font color=\"#00FFFF\">"+ e.Name+ "</font> "+ e.Message);
					break;
			}
		}
	}
}
